package com.sptpc.demo.global.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

//统一的json响应函数
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class GlobalResponse {
    private  Integer code;
    private  String msg;
    private  Object data;

//    带返回值的成功
    public static GlobalResponse success(Object data){
        return GlobalResponse.builder()
                .code(BusinessStatusCode.SUCCESS.getCode())
                .msg(BusinessStatusCode.SUCCESS.getMsg())
                .data(data)
                .build();
    }
//    不带返回值的成功
    public static GlobalResponse success(){
        return success(null);
    }
//    带具体业务错误的失败
    public static GlobalResponse fail(BusinessStatusCode error){
        return GlobalResponse.builder()
                .code(error.getCode())
                .msg(error.getMsg())
                .data(null)
                .build();
    }

}
